package com.bra.ui.activity;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bra.R;
import com.bra.request.UrlConstants;
import com.bra.ui.base.BaseActivity;
import com.bra.utils.CameraUtilsLister;
import com.bra.utils.CommonUtils;
import com.bra.utils.DataCleanManager;
import com.bra.utils.DialogUtil;
import com.bra.utils.ImageUtil;
import com.bra.utils.SPHelper;
import com.bra.utils.ToastUtil;
import com.bra.view.RoundImageView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * 个人资料界面
 * 
 * @author lenovo
 *
 */
public class PersionMessageActivity extends BaseActivity {
	private ImageView activity_back;
	private TextView activity_title;
	private EditText persion_name, persion_SEX, persion_CITY, persion_bir,
			persion_qq;
	private RoundImageView persion_im;
	private Button persion_btu;
	private CameraUtilsLister camera;
	private String PersionUrl = UrlConstants.IP;
	private RequestQueue mRequestQueue;
	private String userId, token;

	@Override
	protected View onCreateView(LayoutInflater inflater,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.activity_persionmessage, null);
	}

	@Override
	protected void onViewCreated(Bundle savedInstanceState) {
		activity_title = (TextView) findViewById(R.id.activity_title);
		activity_back = (ImageView) findViewById(R.id.activity_back);
		activity_title.setText("完善资料");
		persion_im = (RoundImageView) findViewById(R.id.persion_im);
		persion_name = (EditText) findViewById(R.id.persion_name);
		persion_SEX = (EditText) findViewById(R.id.persion_SEX);
		persion_CITY = (EditText) findViewById(R.id.persion_CITY);
		persion_qq = (EditText) findViewById(R.id.persion_qq);
		persion_btu = (Button) findViewById(R.id.persion_btu);
		persion_bir = (EditText) findViewById(R.id.persion_bir);
		userId = SPHelper.readString(this, "userId");
		token = SPHelper.readString(this, "token");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

	}

	@Override
	protected void setListener() {
		activity_back.setOnClickListener(click);
		persion_im.setOnClickListener(click);
		persion_btu.setOnClickListener(click);
	}

	private String p_name, sex, city, number, qq, birth;
	private View.OnClickListener click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.activity_back:
				finish();
				break;
			case R.id.persion_im:
				camera = new CameraUtilsLister(PersionMessageActivity.this);
				OnClickListener camera_lister = camera.camera_lister;
				String[] args = { "拍照", "相册" };
				DialogUtil.showSelectDialog(PersionMessageActivity.this, "请选择",
						args, 0, camera_lister);
				break;
			case R.id.persion_btu:
				p_name = persion_name.getText().toString().trim();
				sex = persion_SEX.getText().toString().trim();
				city = persion_CITY.getText().toString().trim();
				qq = persion_qq.getText().toString().trim();
				birth = persion_bir.getText().toString().trim();
				httpRequestPersionInfo(PersionMessageActivity.this, PersionUrl,
						p_name, sex, city, city, qq, birth);
				Toast.makeText(PersionMessageActivity.this, "中华小子点击了", 0).show();
				break;
			default:
				break;
			}

		}
	};

	private void httpRequestPersionInfo(Context context, String url,
			String p_name, String sex, String province, String city, String qq,
			String birth) {
		AsyncHttpClient client = new AsyncHttpClient();
		// 创建请求参数
		RequestParams PersionInfoparams = new RequestParams();
		PersionInfoparams.put("userId", userId);
		PersionInfoparams.put("equipment", "android");
		PersionInfoparams.put("requestType", "PrefectUserInfo_Api");
		PersionInfoparams.put("token", token);
		PersionInfoparams.put("nickName", p_name);
		PersionInfoparams.put("birthday", birth);
		PersionInfoparams.put("province", province);
		PersionInfoparams.put("city", city);
		PersionInfoparams.put("qq", qq);
		PersionInfoparams.put("sex", sex);

		// 执行post方法
		client.post(url, PersionInfoparams, new JsonHttpResponseHandler() {

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				// TODO Auto-generated method stub
				super.onFailure(statusCode, headers, throwable, errorResponse);
				Toast.makeText(PersionMessageActivity.this, "网络错误", 0).show();
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				// TODO Auto-generated method stub
				super.onSuccess(statusCode, headers, response);
				System.out.println(response.toString() + "====");
				Toast.makeText(PersionMessageActivity.this, response.toString(), 0)
						.show();
			}

		});

	}

	private void httpRequesttFileUploadsInfo(Context context, String url,
			String userId, String token, File file) {

		AsyncHttpClient client = new AsyncHttpClient();
		// 创建请求参数
		RequestParams params = new RequestParams();
		params.put("userId", userId);
		params.put("equipment", "android");
		params.put("requestType", "FileUpload_Api");
		params.put("token", token);
		try {
			params.put("file", file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		params.put("tradeType", "_userPhoto");
		// 执行post方法
		client.post(url, params, new JsonHttpResponseHandler() {

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				// TODO Auto-generated method stub
				super.onFailure(statusCode, headers, throwable, errorResponse);
				Toast.makeText(PersionMessageActivity.this, "网络错误", 0).show();
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				// TODO Auto-generated method stub
				super.onSuccess(statusCode, headers, response);
				System.out.println(response.toString() + "====");
				Toast.makeText(PersionMessageActivity.this, response.toString(), 0)
						.show();
			}

		});
	}

	/**
	 * 用户头像
	 * */
	private File userfaceFile;
	public static String SDCARD = Environment.getExternalStorageDirectory()
			+ "/Android/data/evmanager/iamge/";
	public Uri originalUri;
	private static final int TAKE_PICTURE = 0;
	private static final int CHOOSE_PICTURE = 1;
	private static final int PHOTOSIZE = 1024 * 1024;
	private Bitmap bitmap;
	String name;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		name = String.valueOf(System.currentTimeMillis());
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case TAKE_PICTURE:
				bitmap = camera.getRightBitmap();
				if (bitmap != null) {
					persion_im.setImageBitmap(bitmap);
					userfaceFile = new File(SDCARD + name + ".jpg");
					ImageUtil.savePhotoToSDCard(bitmap, SDCARD, name);
					// 图片上传
					httpRequesttFileUploadsInfo(PersionMessageActivity.this,
							PersionUrl, userId, token, userfaceFile);
				} else {
					ToastUtil.showToast(PersionMessageActivity.this,
							"保存照片失败,请稍后重试");
				}
				break;

			case CHOOSE_PICTURE:
				// 照片的原始资源地址
				originalUri = data.getData();
				originalUri = data.getData();
				bitmap = camera.getRightBitmap(originalUri);
				// 打印出图片的宽和高
				if (bitmap != null) {
					persion_im.setImageBitmap(bitmap);
					userfaceFile = new File(SDCARD + name + ".jpg");
					ImageUtil.savePhotoToSDCard(bitmap, SDCARD, name);
					// 图片上传
					httpRequesttFileUploadsInfo(PersionMessageActivity.this,
							PersionUrl, userId, token, userfaceFile);
				} else {
					ToastUtil.showToast(PersionMessageActivity.this,
							"保存照片失败,请稍后重试");
				}
				break;
			default:
				break;
			}
		}
	}
}